/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.srcptit.trainning.thompv.BaitapMuc1_2;
import java.util.Scanner;
/**
 *
 * @author SONY
 */
public class Bai2Muc2 {
    public static void dem(int n, int a[],int c[])
    {
        for(int i=1;i<n;i++)
            for(int j=i+1;j<=n;j++)
                if(a[i]==a[j]&&c[j]!=0)
                {
                    c[i]++; c[j]=0;
                }
        for(int i=1;i<=n;i++)
            if(c[i]!=0)
            System.out.println(a[i]+" xuat hien "+c[i]+" lan");
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n,stop=1;
        String s;
        System.out.print("Nhap so luong phan tu: ");
        while(stop==1)
        {
            try
            {
                s=sc.nextLine();
                if(s.length()<1) throw new Exception("");
                n=Integer.parseInt(s);
                if(n<=0||n>1000) throw new Exception("");
                stop=0;
                int []a=new int[1000];
                int []c=new int[1000];
                System.out.println("Nhap cac phan tu cua mang: ");
                for(int i=1;i<=n;i++)
                {
                    System.out.print("a["+i+"]=");
                    a[i]=sc.nextInt();
                    c[i]=1;
                }
                System.out.println("Ket qua:");
                dem(n,a,c);
            }
            catch (Exception e)
            {
                System.out.println("Nhap lai:");
            }
        }
    }
}
